import { defineStore } from 'pinia'
import { Router } from '@/service/common/interface'
import CommonService from '@/service/common'
import router from '../router'
import { toHump } from '_/utils'
import User from '@/entity/system/user/User'
import { RouteLocationNormalizedGeneric } from 'vue-router'

interface AppState {
    menuCollapse: boolean
    defaultActive: string
    routers: Router[]
    storeRouters: RouteLocationNormalizedGeneric[]
    dictData: any
    urlSelectData: any
    userInfo: User
}

const commonService = new CommonService()

const useApp = defineStore('appStore', {
    state(): AppState {
        return {
            menuCollapse: false,
            defaultActive: '/home',
            routers: [],
            storeRouters: [], // 已经跳转过的路由，页面顶部tabs
            userInfo: new User(),
            dictData: {}, // 缓存字典数据
            urlSelectData: {} // 缓存下拉选择框数据
        }
    },
    actions: {
        async initRouters() {
            const res = await commonService.getRouters({})
            if (res.code === 200) {
                this.routers = res.data
                this.genRouter(this.routers)
            }
        },
        genRouter(vals: Router[]) {
            let modules = import.meta.glob(`../../views/module/**/*.vue`)
            // 递归生成路由
            function innerGen(accept: Router[], parent?: Router) {
                accept.forEach((val: Router) => {
                    const vuefile = `../../views/module/${val.component}/IndexView.vue`
                    if (parent) {
                        // 把父级的path继承下来
                        if (val.path.startsWith('/')) {
                            val.fullpath = (parent.fullpath ? parent.fullpath : '') + val.path
                        } else {
                            val.fullpath = (parent.fullpath ? parent.fullpath + '/' : '') + val.path
                        }
                        val.fullname = toHump(val.fullpath)
                    } else {
                        val.fullpath = val.path
                        val.fullname = toHump(val.path)
                    }

                    if (modules[vuefile]) {
                        // 本地存在此文件，则把该页面注册至路由
                        const menu: any = {
                            path: val.fullpath,
                            name: val.fullname,
                            component: modules[vuefile],
                            meta: val.meta,
                        }
                        router.addRoute("Main", menu)
                    }
                    if (val.children) {
                        innerGen(val.children, val)
                    }
                })
            }
            innerGen(vals)

            router.addRoute(
                {
                    path: "/:pathMatch(.*)",
                    redirect: "/"
                }
            )
        },
        async initUserInfo() {
            const data = await commonService.getUserInfo({})
            if (data.code === 200) {
                this.userInfo = data.user
            }
        }
    }
})

export default useApp